<html>
	<head>
		<script src="../../build/yahoo/yahoo-min.js"></script>
		<script src="../../build/connection/connection-min.js"></script>
		<link rel="stylesheet" type="text/css" href="../../docs/assets/dpSyntaxHighlighter.css" />
		<style>
		fieldset {
			width: 500px;
		}
		</style>
	</head>
<body>

<h1> Connection Manager Transaction Timeout</h1>
<p>
The following code example provides a step-by-step approach to presetting a transaction timeout.
</p>

<h3>Source file and dependencies</h3>
<p>Load the YAHOO namespace and connection manager source file:</p>

<pre><textarea name="code" class="JScript" cols="60" rows="1">
<script src="yahoo.js"></script>
<script src="connection.js"></script>
</textarea></pre>

<h3>The Callback Object</h3>
<p>The callback object includes a new property: timeout. To enable a transaction to automatically timeout, the timeout property must be defined wih a value in millseconds. This example defines timeout with a value of 5000(milliseconds). If the transaction is not complete by 5000ms, it will be aborted.</p>
<pre>
<textarea name="code" class="JScript" cols="60" rows="1">
var handleSuccess = function(o){
	if(o.responseText !== undefined){
		div.innerHTML = "Transaction id: " + o.tId;
		div.innerHTML += "HTTP status: " + o.status;
		div.innerHTML += "Server response: " + o.responseText;
		div.innerHTML += "Argument object: property foo = " + o.argument.foo +
						 "and property bar = " + o.argument.bar;
	}
}

var handleFailure = function(o){
	div.innerHTML += "<li>Transaction id: " + o.tId + "</li>";
	div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
	div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
}

var callback =
{
  success:handleSuccess,
  failure: handleFailure,
  argument: { foo:"foo", bar:"bar" },
  timeout: 5000
};
</textarea>
</pre>

<h3>Initiate the Transaction</h3>
<p>
Call YAHOO.util.Connect.asyncRequest to send the request to sync.php. The PHP file will return a string message after a random delay of 0 to 10 seconds, if the transaction was not aborted. If the transaction was successfully aborted, the response object's status property will report -1 and the statusText property will report "transaction aborted".
</p>

<pre><textarea name="code" class="JScript" cols="60" rows="1">
var sUrl = "php/sync.php";
var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
</textarea></pre>

<div id="container"></div>
<script>

var div = document.getElementById('container');

var handleSuccess = function(o){
	if(o.responseText !== undefined){
		div.innerHTML += "<li>Transaction id: " + o.tId + "</li>";
		div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
		div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
		div.innerHTML += "<li>HTTP headers: <ul>" + o.getAllResponseHeaders + "</ul></li>";
		div.innerHTML += "<li>Server response: " + o.responseText + "</li>";
		div.innerHTML += "<li>Argument object: Object ( [foo] => " + o.argument.foo +
						 " [bar] => " + o.argument.bar +" )</li>";
	}
}

var handleFailure = function(o){
	div.innerHTML += "<li>Transaction id: " + o.tId + "</li>";
	div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
	div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
}

var callback =
{
  success: handleSuccess,
  failure: handleFailure,
  argument: { foo:"foo", bar:"bar" },
  timeout: 5000
};

var sUrl = 'php/sync.php';

function makeRequest(){
	var obj1 = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
	var obj2 = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
}

</script>
<form><input type="button" value="Create Two Transactions" onClick="makeRequest();"></form>
<script src="../../examples/assets/dpSyntaxHighlighter.js"></script>
<script language="javascript">
dp.SyntaxHighlighter.HighlightAll('code');
</script>
</body>
</html>